package com.yhthu;

import com.yhthu.bean.FilePath;
import com.yhthu.bean.Instruction;
import com.yhthu.bean.Record;
import com.yhthu.query.BaseQuery;
import com.yhthu.query.QueryFactory;
import com.yhthu.splitstrategy.BlankLineSpliter;
import com.yhthu.splitstrategy.SpaceSpliter;
import com.yhthu.splitstrategy.WrapSpliter;
import com.yhthu.texthandler.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class DM {

    public static void main(String[] args) {
        // 判断是否有正常输入
        if (args == null || args.length != 4) {
            System.out.println("请指定正确的文件路径");
            return;
        }
        // 解析用户输入的文件地址
        DM dm = new DM();
        FilePath filePath = new FilePath(args);

        // 1. 从Record文件中解析所有的记录
        List<Record> recordList = dm.getValidRecordsFromFilePath(filePath.getRecordFilePath());
        // 2. 从Instruction文件中解析所有指令
        List<Instruction> instructionList = dm.getValidInstructionFromFilePath(filePath.getInstructionFilePath());
        // 3. 根据指令获取报告
        List<List<Record>> queryReport = dm.getQueryReport(recordList, instructionList);
//        // 4. 写Result文件和Report文件
//        if (!dm.writeRecords2File(recordList, filePath.getResultFilePath())) {
//            System.out.println("写Result文件失败，请重试");
//        }
//        if (!dm.writeRecords2File(queryReport, filePath.getReportFilePath())) {
//            System.out.println("写Report文件失败，请重试");
//        }
    }

    /**
     * 根据记录文件地址获取合法的记录
     *
     * @param recordFilePath 记录文件路径
     * @return 合法的记录
     */
    public List<Record> getValidRecordsFromFilePath(String recordFilePath) {
        BaseHandler<Record> recordSpliter = new RecordHandler();
        // 分别设置拆分策略
        recordSpliter.setSingleSplitStrategy(new BlankLineSpliter());
        recordSpliter.setLineSplitStrategy(new WrapSpliter());
        recordSpliter.setWordSplitStrategy(new SpaceSpliter());
        // 从文件路径读取字符串
        try {
            String recordContent = new String(Files.readAllBytes(Paths.get(recordFilePath)));
            return recordSpliter.getValidObjectFromTextContent(recordContent);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据指令文件地址解析指令
     *
     * @param instructionFilePath
     * @return
     */
    public List<Instruction> getValidInstructionFromFilePath(String instructionFilePath) {
        BaseHandler<Instruction> instructionSpliter = new InstructionHandler();
        instructionSpliter.setLineSplitStrategy(new WrapSpliter());
        instructionSpliter.setWordSplitStrategy(new SpaceSpliter());
        // 从文件路径读取字符串
        try {
            String instructionContent = new String(Files.readAllBytes(Paths.get(instructionFilePath)));
            return instructionSpliter.getValidObjectFromTextContent(instructionContent);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 针对记录列表做对应查询指令操作
     *
     * @param sourceRecords
     * @param instructions
     * @return
     */
    public List<List<Record>> getQueryReport(List<Record> sourceRecords, List<Instruction> instructions) {
        List<List<Record>> reportRecordList = new ArrayList<>();
        QueryFactory queryFactory = new QueryFactory();
        for (Instruction instruction : instructions) {
            BaseQuery query = queryFactory.getQuery(instruction);
            reportRecordList.add(query.getQueryReport(sourceRecords, instructions));
        }
        return reportRecordList;
    }

    private boolean writeRecords2File(List<Record> records, String filePath) {
        return true;
    }
}
